{
数据结构 解题报告：
http://git.oschina.net/et0662/sznoi-Data_Structure
}

type pnode=^node;
	node=record
		xs:integer;
		zs:integer;
		next:pnode;
	end;
	Link=^node;


{打印 链接表的 过程}
procedure print(y:Link);
var
	p:pnode;
	flag:boolean;
begin
	flag:=false;
	p:=y;
	while(p^.next<>nil) do
	begin
		p:=p^.next;
		if(p^.xs<>0) then
		begin
			flag:=true;
			write(p^.xs,' ',p^.zs,' ');
		end
	end;
	if not flag then
		write(0);
end;

{插入一个项}
procedure ins(y:Link;p:pNode);
var
	p0:pnode;
begin
	p0:=y;
	while(p0^.next<>nil)do
	begin
		if( p0^.next^.zs = p^.zs)then
		begin
			p0^.next^.xs:=p0^.next^.xs+p^.xs;
			exit;
		end
		else if ( p0^.next^.zs > p^.zs) then
		begin
			p0:=p0^.next;
		end
		else
			break;
	end;
	p^.next:=p0^.next;
	p0^.next:=p;
end;
	

{链表相加~}
function add(y1,y2:Link):Link;
var
	y,p,p1,p2,p3:pnode;
begin
	new(y);
	y^.next:=nil;
	p3:=y;
	p1:=y1;
	p2:=y2;
	while (p1^.next<>nil) and (p2^.next<>nil) do
	begin
		new(p);
		p^.next:=nil;
		if p1^.next^.zs = p2^.next^.zs then
		begin
			p^.zs:=p1^.next^.zs;
			p^.xs:=p1^.next^.xs+p2^.next^.xs;
			p1:=p1^.next;
			p2:=p2^.next;
		end
		else if p1^.next^.zs > p2^.next^.zs then
		begin
			p^.zs:=p1^.next^.zs;
			p^.xs:=p1^.next^.xs;
			p1:=p1^.next;
		end
		else
		begin
			p^.zs:=p2^.next^.zs;
			p^.xs:=p2^.next^.xs;
			p2:=p2^.next;
		end;
		p3^.next:=p;
		p3:=p3^.next;
	end;

	while (p1^.next<>nil) do
	begin
		new(p);
		p^.next:=nil;
		p^.zs:=p1^.next^.zs;
		p^.xs:=p1^.next^.xs;
		p1:=p1^.next;
		p3^.next:=p;
		p3:=p3^.next;
	end;

	while (p2^.next<>nil) do
	begin
		new(p);
		p^.next:=nil;
		p^.zs:=p2^.next^.zs;
		p^.xs:=p2^.next^.xs;
		p2:=p2^.next;
		p3^.next:=p;
		p3:=p3^.next;
	end;
	add:=y;
end;

{main}
var
	t1,t2:integer;
	y1,y2,y3:Link;
	p:pnode;
begin
	new(y1);
	y1^.next:=nil;
	while not eoln do
	begin
		read(t1,t2);
		new(p);
		p^.next:=nil;
		p^.xs:=t1;
		p^.zs:=t2;
		ins(y1,p);
	end;
	
	readln;
	new(y2);
	y2^.next:=nil;
	while not eoln do
	begin
		read(t1,t2);
		new(p);
		p^.next:=nil;
		p^.xs:=t1;
		p^.zs:=t2;
		ins(y2,p);
	end;
	
	y3:=add(y1,y2);
	
	print(y3);
end.
